package com.bd.scm.module.qywx.pojo.Util;

import com.alibaba.fastjson.JSONObject;
import com.bd.scm.module.qywx.thread.TokenThread;
import com.bd.scm.module.utils.wx.WXAuthUtil;
import com.bd.wx.core.util.WeiXinParamesUtil;
import com.bd.wx.core.util.WeiXinUtil;
import com.bd.wx.core.util.message.send.*;
import com.bd.wx.pojo.service.SendMessageService;

import java.util.ArrayList;
import java.util.List;

/**@desc  : 消息推送之发送消息
 *
 * @author: shirayner
 * @date  : 2017-8-18 上午10:04:55
 */
public class SendMessageUtil {

    //1.发送文本消息
    @SuppressWarnings("static-access")
	public static void testSendTextMessage(String content,String touser) throws Exception{
        	//0.设置消息内容
//          String content="你的快递已到，请携带工卡前往邮件中心领取。\n出发前可查看" +
//                  "<a href=\"http://work.weixin.qq.com\">邮件中心视频实况" +
//                  "</a>，聪明避开排队。";

          //1.创建文本消息对象
          TextMessage message=new TextMessage();
          //1.1非必需
          message.setTouser(touser);  //不区分大小写
          //textMessage.setToparty("1");
          //txtMsg.setTotag(totag);
          //txtMsg.setSafe(0);

          //1.2必需
          message.setMsgtype("text");
          message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));
System.out.println(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId"))+" ===== "+ TokenThread.accessToken.getToken());
          Text text=new Text();
          text.setContent(content);
          message.setText(text);

          //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
//          String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.getProperty("corpId"), WeiXinParamesUtil.getProperty("agentSecret")).getToken();
//          System.out.println("accessToken:"+accessToken);

          //3.发送消息：调用业务类，发送消息
          SendMessageService sms=new SendMessageService();
          sms.sendMessage(TokenThread.accessToken.getToken(), message);
    }

    /**
     * 访客预约发送信息给被访人
     * @param jsonObject
     * @throws Exception
     */
    //2.发送文本卡片消息
    public synchronized static void testSendTextcardMessage(JSONObject jsonObject) throws Exception{
      //0.设置消息内容
        String title="访客预约通知:";
        String description="<div class=\"normal\">\n"+jsonObject.getString("applyUserName")+"，您好：\r\n</div><div class=\"normal\">" +
                jsonObject.getString("companyName")+"  "+jsonObject.getString("visitorName")+" 于 "+jsonObject.getString("startDate")+" 至 "+jsonObject.getString("endDate")+" 来访</div><div class=\"normal\">" +
                "</div>";
        String url= WXAuthUtil.frontPath+"Details?billId="+jsonObject.getString("id");

        //1.创建文本卡片消息对象
        TextcardMessage message=new TextcardMessage();
        //1.1非必需
        message.setTouser(jsonObject.getString("weChatId"));  //不区分大小写
        //message.setToparty("1");
        //message.setTotag(totag);
        //message.setSafe(0);

        //1.2必需
        message.setMsgtype("textcard");
        message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));

        Textcard textcard=new Textcard();
        textcard.setTitle(title);
        textcard.setDescription(description);
        textcard.setUrl(url);
        message.setTextcard(textcard);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        //String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.getProperty("corpId"), WeiXinParamesUtil.getProperty("agentSecret")).getToken();
        //System.out.println("accessToken:"+accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms=new SendMessageService();
        sms.sendMessage(TokenThread.accessToken.getToken(), message);
    }

    /**
     * 这是发送给审批人信息  一个 8点 12点
     *  一个 12点 15点
     * @param jsonObject
     * @throws Exception
     */
    public synchronized static void testSendTextcardMessage2(JSONObject jsonObject) throws Exception{
      //0.设置消息内容
        String title="新访单通知:";
        String description="<div class=\"normal\">\n"+jsonObject.getString("userLname")+"，您好：\r\n</div><div class=\"normal\">" +
                ""+jsonObject.getString("startDate")+"至"+jsonObject.getString("endDate")+"期间，您有新的访单通知。</div>";
        String url= WeiXinParamesUtil.getProperty("projectPath")+"/visitor/newVisitorListNotification?userLname="+jsonObject.getString("userLname")+"&startDate="+jsonObject.getString("startDateTime")+"&endDate="+jsonObject.getString("endDateTime")+"&roleBsid="+jsonObject.getString("roleBsid");

        //1.创建文本卡片消息对象
        TextcardMessage message=new TextcardMessage();
        //1.1非必需
        message.setTouser(jsonObject.getString("userFname"));  //不区分大小写
        //message.setToparty("1");
        //message.setTotag(totag);
        //message.setSafe(0);

        //1.2必需
        message.setMsgtype("textcard");
        message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));

        Textcard textcard=new Textcard();
        textcard.setTitle(title);
        textcard.setDescription(description);
        textcard.setUrl(url);
        message.setTextcard(textcard);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        //String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.getProperty("corpId"), WeiXinParamesUtil.getProperty("agentSecret")).getToken();
        //System.out.println("accessToken:"+accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms=new SendMessageService();
        sms.sendMessage(TokenThread.accessToken.getToken(), message);
    }

    //3.发送图片消息---无效的media_id
    public static void testSendImgMessage(String fromUserName) throws Exception{
        //0.设置消息内容
        String media_id="2Wf9TMCj9LPkJu3Otl8aWvRuP7l_gSsqT8AbuGjRrenw";
        //1.创建图片消息对象
        ImgMessage message=new ImgMessage();
        //1.1非必需
        message.setTouser(fromUserName);  //不区分大小写
        //textMessage.setToparty("1");
        //txtMsg.setTotag(totag);
        //txtMsg.setSafe(0);

        //1.2必需
        message.setMsgtype("image");
        message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));

        Media image=new Media();
        image.setMedia_id(media_id);
        message.setImage(image);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        String accessToken= TokenThread.accessToken.getToken();
        System.out.println("accessToken:"+accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms=new SendMessageService();
        sms.sendMessage(accessToken, message);

    }


    //4.发送语音消息---无效的media_id
    public void testSendVoiceMessage() throws Exception{
        //0.设置消息内容
        String media_id="MEDIA_ID";
        //1.创建语音消息对象
        VoiceMessage message=new VoiceMessage();
        //1.1非必需
        message.setTouser("@all");  //不区分大小写
        //textMessage.setToparty("1");
        //txtMsg.setTotag(totag);
        //txtMsg.setSafe(0);

        //1.2必需
        message.setMsgtype("image");
        message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));

        Media voice=new Media();
        voice.setMedia_id(media_id);
        message.setVoice(voice);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.getProperty("corpId"), WeiXinParamesUtil.getProperty("agentSecret")).getToken();
        System.out.println("accessToken:"+accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms=new SendMessageService();
        sms.sendMessage(accessToken, message);

    }

    //5.发送视频消息
    public void testSendVideoMessage() throws Exception{
        //0.设置消息内容
        String media_id="MEDIA_ID";
        String title="视频示例";
        String description="好看的视频";


        //1.创建视频消息对象
        VideoMessage message=new VideoMessage();
        //1.1非必需
        message.setTouser("@all");  //不区分大小写
        //message.setToparty("1");
        //message.setTotag(totag);
        //message.setSafe(0);

        //1.2必需
        message.setMsgtype("video");
        message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));

        Video video=new Video();
        video.setMedia_id(media_id);
        video.setTitle(title);
        video.setDescription(description);
        message.setVideo(video);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.getProperty("corpId"), WeiXinParamesUtil.getProperty("agentSecret")).getToken();
        System.out.println("accessToken:"+accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms=new SendMessageService();
        sms.sendMessage(accessToken, message);

    }

    //6.发送文件消息
    public void testSendFileMessage() throws Exception{
        //0.设置消息内容
        String media_id="MEDIA_ID";

        //1.创建文件对象
        FileMessage message=new FileMessage();
        //1.1非必需
        message.setTouser("@all");  //不区分大小写
        //textMessage.setToparty("1");
        //txtMsg.setTotag(totag);
        //txtMsg.setSafe(0);

        //1.2必需
        message.setMsgtype("file");
        message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));

        Media file=new Media();
        file.setMedia_id(media_id);
        message.setFile(file);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        String accessToken= WeiXinUtil.getAccessToken(WeiXinParamesUtil.getProperty("corpId"), WeiXinParamesUtil.getProperty("agentSecret")).getToken();
        System.out.println("accessToken:"+accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms=new SendMessageService();
        sms.sendMessage(accessToken, message);

    }

    //7.发送图文消息
    public static void testSendNewsMessage(String fromUserName) throws Exception{

        //1.创建图文消息对象
        NewsMessage message=new NewsMessage();
        //1.1非必需
        message.setTouser(fromUserName);  //不区分大小写
        //textMessage.setToparty("1");
        //txtMsg.setTotag(totag);
        //txtMsg.setSafe(0);

        //1.2必需
        message.setMsgtype("news");
        message.setAgentid(Integer.valueOf(WeiXinParamesUtil.getProperty("agentId")));
        //设置图文消息
        Article article1=new Article();
        article1.setTitle("访客预约系统操作说明");
        article1.setDescription("访客预约系统内回复‘ 操作说明 ’获取此推送信息");
        article1.setPicurl(WeiXinParamesUtil.getProperty("projectPath")+"/logo001.jpg");
        article1.setUrl(WeiXinParamesUtil.getProperty("projectPath")+"/caozuoshuoming.html");

        List<Article>  articles=new ArrayList<Article>();
        articles.add(article1);

        News news=new News();
        news.setArticles(articles);
        message.setNews(news);

        //2.获取access_token:根据企业id和通讯录密钥获取access_token,并拼接请求url
        String accessToken= TokenThread.accessToken.getToken();
        System.out.println("accessToken:"+accessToken);

        //3.发送消息：调用业务类，发送消息
        SendMessageService sms=new SendMessageService();
        sms.sendMessage(accessToken, message);
    }




}
